package hos.okgo.converter;

import com.lzy.okgo.convert.Converter;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Response;

/**
 * <p>Title: OkGoConverter </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/2/19 22:53
 */
public abstract class OkGoConverter<T> implements Converter<T> {

    private final Type type;

    public OkGoConverter() {
        type = getType();
    }

    public OkGoConverter(Type type) {
        this.type = type;
    }

    public Type getType() {
        Type genType = getClass().getGenericSuperclass();
        if (genType != null) {
            return ((ParameterizedType) genType).getActualTypeArguments()[0];
        }
        return null;
    }

    @Override
    public T convertResponse(Response response) throws Throwable {
        return convertResponse(type, response);
    }

    public abstract T convertResponse(Type succeed, Response response) throws IOException;
}
